<?php
mysql_connect("127.0.0.1","root","sd2013");
mysql_select_db("mathRomp");

//Create new game in GAME database
$prob = mysql_real_escape_string($_REQUEST['prob']);
print($prob);
$q=mysql_query("INSERT INTO `Game`(`gameId`, `status`, `currentProb`, `turn`, `gramType`, `subject`) VALUES (NULL, 0, '".$prob."', '".$_REQUEST['userId1']."', ".$_REQUEST['gramType'].", '".$_REQUEST['probType']."')");
while($e=mysql_fetch_assoc($q)){
  $output[]=$e;
}
print(json_encode($output));
$id = mysql_insert_id();
//print($id);

//Add user1 to GAMES-USERS database
$q2=mysql_query("INSERT INTO `Games-Users`(`gameId`, `userId`, `userScore`, `userNum`) VALUES (".$id.", ".$_REQUEST['userId1'].", 0, 0)");
while($f=mysql_fetch_assoc($q2)){
  $output2[]=$f;
}
//print(json_encode($output2));

//Get user2's id from USERS database
$q3=mysql_query("SELECT `userId` FROM users WHERE username='".$_REQUEST['userName']."'");
$result = mysql_result($q3, 0);
while($g=mysql_fetch_assoc($q3)){
  $output3[]=$g;
}
//print(json_encode($output3));

//Add user2 to GAMES-USERS database
$q4=mysql_query("INSERT INTO `Games-Users`(`gameId`, `userId`, `userScore`, `userNum`) VALUES (".$id.", ".$result.", 0, 1)");
while($h=mysql_fetch_assoc($q4)){
  $output4[]=$h;
}
//print(json_encode($output4));

$q5=mysql_query("SELECT * FROM `Game` WHERE gameId=".$id."");
while($j=mysql_fetch_assoc($q5)){
  $output5[]=$j;
}
print(json_encode($output5));
//mysql_close();
?>